﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Acme.BookStore2.EntityFrameworkCore
{
    public class WorkScheduleRepository : EfCoreRepository<BookStore2DbContext, WorkSchedule, Guid>, IWorkScheduleRepository
    {
        public WorkScheduleRepository(IDbContextProvider<BookStore2DbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
        
        public async Task<List<WorkSchedule>> GetPagedListAsync(
            string sorting = null,
            int maxResultCount = int.MaxValue,
            int skipCount = 0,
            DateTime? creationTimeBegin = null,
            DateTime? creationTimeEnd = null,
            bool includeDetails = false,
            CancellationToken cancellationToken = default)
        {
            return await DbSet
                .WhereIf(creationTimeBegin.HasValue, v => v.CreationTime >= creationTimeBegin)
                .WhereIf(creationTimeEnd.HasValue, v => v.CreationTime <= creationTimeEnd)
                .OrderBy(sorting ?? nameof(VehicleDriveRecord.CreationTime) + " desc")
                .PageBy(skipCount, maxResultCount)
                .ToListAsync(GetCancellationToken(cancellationToken));
        }


        public async Task<long> GetCountAsync(
            DateTime? creationTimeBegin = null,
            DateTime? creationTimeEnd = null,
            CancellationToken cancellationToken = default)
        {
            return await this
                .WhereIf(creationTimeBegin.HasValue, v => v.CreationTime >= creationTimeBegin)
                .WhereIf(creationTimeEnd.HasValue, v => v.CreationTime <= creationTimeEnd)
                .LongCountAsync(GetCancellationToken(cancellationToken));
        }
    }
    
    public class QuestionAndAnswerPostRepository : EfCoreRepository<BookStore2DbContext, QuestionAndAnswerPost, Guid>, IQuestionAndAnswerPostRepository
    {
        public QuestionAndAnswerPostRepository(IDbContextProvider<BookStore2DbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
        
        public async Task<List<QuestionAndAnswerPost>> GetPagedListAsync(
            string sorting = null,
            int maxResultCount = int.MaxValue,
            int skipCount = 0,
            string title = null,
            bool includeDetails = false,
            CancellationToken cancellationToken = default)
        {
            return await DbSet
                .WhereIf(!title.IsNullOrWhiteSpace(), v => v.Title == title)
                .OrderBy(sorting ?? nameof(VehicleDriveRecord.CreationTime) + " desc")
                .PageBy(skipCount, maxResultCount)
                .ToListAsync(GetCancellationToken(cancellationToken));
        }


        public async Task<long> GetCountAsync(
            string title = null,
            CancellationToken cancellationToken = default)
        {
            return await this
                .WhereIf(!title.IsNullOrWhiteSpace(), v => v.Title == title)
                .LongCountAsync(GetCancellationToken(cancellationToken));
        }
    }
}